package com.miyu.common.utils.ehcache;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSONObject;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import java.net.URL;

/**
 *增加key有效期限 额外扩展字段
 */
public class EhcacheUtil {
    private static final String path = "/ehcache.xml";

    private URL url;

    private CacheManager manager;

    private static EhcacheUtil ehCache;

    private EhcacheUtil(String path) {
        url = getClass().getResource(path);
        manager = CacheManager.create(url);
    }
    public static EhcacheUtil getInstance() {
        if (ehCache== null) {
            ehCache= new EhcacheUtil(path);
        }
        return ehCache;
    }

    /**
     * 数据缓存
     * @param cacheName
     * @param key
     * @param value
     */
    public void put(String cacheName, String key, Object value) {
        /*默认永久*/
        long expiry =-1;
        JSONObject jo = new JSONObject();
        Cache cache = manager.getCache(cacheName);
        jo.put("data",value);
        jo.put("cacheDate", DateUtil.currentSeconds());
        /*过期时间*/
        jo.put("expiry",expiry);
        Element element = new Element(key, jo);
        cache.put(element);
    }

    /**
     *
     * @param cacheName
     * @param key
     * @param value
     * @param expiry 有效期限
     */
    public void put(String cacheName, String key, Object value,long expiry) {
        JSONObject jo = new JSONObject();
        jo.put("cacheDate", DateUtil.currentSeconds());
        /*过期时间*/
        jo.put("expiry",expiry);
        jo.put("data",value);
        Cache cache = manager.getCache(cacheName);
        Element element = new Element(key, jo);
        cache.put(element);
    }

    /**
     * 增加默认cache 使用 没有cachename
     * 传入时适应默认 defultCache 空间
     * @param key
     * @param value
     */
    public void put( String key, Object value) {
        /*默认永久*/
        long expiry =-1;
        JSONObject jo = new JSONObject();
        Cache cache = manager.getCache("defultCache");
        jo.put("data",value);
        jo.put("cacheDate", DateUtil.currentSeconds());
        /*过期时间*/
        jo.put("expiry",expiry);
        Element element = new Element(key, jo);
        cache.put(element);
    }
    /**
     * 增加默认cache 使用 没有cachename
     * 传入时适应默认 defultCache 空间
     * @param key
     * @param value
     * @param expiry
     */
    public void put(String key, Object value,long expiry) {
        JSONObject jo = new JSONObject();
        jo.put("cacheDate", DateUtil.currentSeconds());
        /*过期时间*/
        jo.put("expiry",expiry);
        jo.put("data",value);
        Cache cache = manager.getCache("defultCache");
        Element element = new Element(key, jo);
        cache.put(element);
    }

    /**
     * 获取缓存
     * @param cacheName
     * @param key
     * @return
     */
    public Object get(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
        if(element==null){
            return null;
        }else{
            Object objectValue = element.getObjectValue();
            JSONObject jo = JSONObject.parseObject(objectValue.toString());
            long expiry = jo.getLong("expiry");
            if (expiry!=-1){
                if(DateUtil.currentSeconds()-jo.getLong("cacheDate")<jo.getLong("expiry")){
                    return jo.get("data");
                }else{
                    /*过期移除*/
                    remove( cacheName, key);
                }
            }else{
              return jo.get("data");
            }
        }
        return null;
    }

    /**
     * 获取缓存
     * 返回同一JSONObject 数据结构
     * @param key
     * @return
     */
    public JSONObject getJSONObject(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        JSONObject jo = new JSONObject();
        Element element = cache.get(key);
        if(element==null){
            return null;
        }else{
            Object objectValue = element.getObjectValue();
            jo = JSONObject.parseObject(objectValue.toString());
            long expiry = jo.getLong("expiry");
            if (expiry!=-1){
                if(DateUtil.currentSeconds()-jo.getLong("cacheDate")<jo.getLong("expiry")){
                    return jo;
                }else{
                    /*过期移除*/
                    remove( cacheName, key);
                }
            }else{
                return jo;
            }
        }
        return null;
    }

    /**
     * 获取缓存
     * @param key
     * @return
     */

    /**
     * 判断是否存在缓存
     * @param cacheName
     * @param key
     * @return
     */
    public boolean hasKey(String cacheName, String key){
        Cache cache = manager.getCache(cacheName);
        Element element = cache.get(key);
        if(element!=null){
            /*判断是否过期*/
            Object objectValue = element.getObjectValue();
            JSONObject jo = JSONObject.parseObject(objectValue.toString());
            long expiry = jo.getLong("expiry");
            if (expiry!=-1){
                if(DateUtil.currentSeconds()-jo.getLong("cacheDate")<jo.getLong("expiry")){
                    return true;
                }else{
                    /*过期移除*/
                    remove( cacheName, key);
                }
            }else{
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否存在缓存
     * @param key
     * @return
     */
    public boolean hasKey( String key){
        Cache cache = manager.getCache("defultCache");
        Element element = cache.get(key);
        if(element!=null){
            /*判断是否过期*/
            Object objectValue = element.getObjectValue();
            JSONObject jo = JSONObject.parseObject(objectValue.toString());
            long expiry = jo.getLong("expiry");
            if (expiry!=-1){
                if(DateUtil.currentSeconds()-jo.getLong("cacheDate")<jo.getLong("expiry")){
                    return true;
                }else{
                    /*过期移除*/
                    remove( "defultCache", key);
                }
            }else{
                return true;
            }
        }
        return false;
    }

    /**
     * 获取缓存所有数据
     * @param cacheName
     * @return
     */
    public Cache get(String cacheName) {
        return manager.getCache(cacheName);
    }

    /**
     * 移除缓存
     * @param cacheName
     * @param key
     */
    public void remove(String cacheName, String key) {
        Cache cache = manager.getCache(cacheName);
        cache.remove(key);
    }

    /**
     * 获取Cache，当Cache不存在时自动创建
     *
     * @param cacheName
     * @return Cache
     * @author netwild@qq.com
     */
    public  Cache getOrAddCache(String cacheName) {
        Cache cache = manager.getCache(cacheName);
        if (cache == null) {
                cache = manager.getCache(cacheName);
                if (cache == null) {
                    manager.addCacheIfAbsent(cacheName);
                    cache = manager.getCache(cacheName);
                }
        }
        return cache;
    }


}
